package com.crawler.util;

import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IPUtils {
	public static String getLocalIPAddress() {
		try {
			String html = HttpResponseUtil
					.getHtml(HttpRequestUtil.getRequestMethod("http://1212.ip138.com/ic.asp", null), "gb2312");
			String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(html);
			String ip = "";
			while (m.find()) {
				ip = m.group();
			}
			return ip;
		} catch (IllegalStateException e) {
			return "0.0.0.0";
		}
	}
	
	public static String getLocalIp(){
		InetAddress ia=null;
		try {
			ia=InetAddress.getLocalHost();
			//String localname=ia.getHostName();
			String localip=ia.getHostAddress();
			//System.out.println("本机的ip是 ："+localip);
			return localip;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) {
		getLocalIp();
	}
}
